CURRENT_DIR = `pwd`
HTMLCXX_PATH = $(CURRENT_DIR)/htmlcxx-0.84
HTMLCXX_INCLUDE_PATH = $(HTMLCXX_PATH)/include
HTMLCXX_LIB_PATH = $(HTMLCXX_PATH)/lib

CXX = g++

CXXFLAGS = -O2 -Wall
LIBSFLAGS = -lz -L $(HTMLCXX_LIB_PATH)
		
INCLUDES = -I riCode/src -I $(HTMLCXX_INCLUDE_PATH)

BINFOLDER = bin
TESTFOLDER = test

COMPILE = $(CXX) $(INCLUDES) -c $(CXXFLAGS)

QUERY_TARGET = $(BINFOLDER)/query
QUERY_OBJS = $(BINFOLDER)/Registry.o \
	$(BINFOLDER)/Run.o \
	$(BINFOLDER)/Lexon.o \
	$(BINFOLDER)/Parser.o \
	$(BINFOLDER)/Document.o \
	$(BINFOLDER)/CollectionReader.o \
	$(BINFOLDER)/IndexBuilder.o \
	$(BINFOLDER)/Index.o \
	$(BINFOLDER)/Encoder.o \
	$(BINFOLDER)/QueryResult.o \
	$(BINFOLDER)/TimeMeasurement.o \
	$(BINFOLDER)/QueryMain.o \
	$(BINFOLDER)/DocumentInfo.o \
	$(BINFOLDER)/DocumentIndex.o \
	$(BINFOLDER)/AnchorText.o
$(QUERY_TARGET):	$(QUERY_OBJS)
	$(CXX) $(QUERY_OBJS) $(HTMLCXX_LIB_PATH)/*.o $(LIBSFLAGS) $(INCLUDES) -o $(QUERY_TARGET)

INDEX_TARGET = $(BINFOLDER)/index 	
INDEX_OBJS = $(BINFOLDER)/Document.o \
	$(BINFOLDER)/CollectionReader.o \
	$(BINFOLDER)/Registry.o \
	$(BINFOLDER)/Run.o \
	$(BINFOLDER)/Lexon.o \
	$(BINFOLDER)/Parser.o \
	$(BINFOLDER)/IndexBuilder.o \
	$(BINFOLDER)/Encoder.o \
	$(BINFOLDER)/Index.o \
	$(BINFOLDER)/QueryResult.o \
	$(BINFOLDER)/TimeMeasurement.o \
	$(BINFOLDER)/IndexMain.o \
	$(BINFOLDER)/DocumentInfo.o \
	$(BINFOLDER)/DocumentIndex.o \
	$(BINFOLDER)/AnchorText.o
$(INDEX_TARGET):	$(INDEX_OBJS)
	$(CXX) $(INDEX_OBJS) $(HTMLCXX_LIB_PATH)/*.o $(LIBSFLAGS) $(INCLUDES) -o $(INDEX_TARGET)

# Collection I/O
$(BINFOLDER)/Document.o: riCode/src/Document.cpp riCode/src/Document.h
	${COMPILE} riCode/src/Document.cpp -o $(BINFOLDER)/Document.o
$(BINFOLDER)/CollectionReader.o: riCode/src/CollectionReader.cpp riCode/src/CollectionReader.h riCode/src/envDefault.h
	${COMPILE} riCode/src/CollectionReader.cpp -o $(BINFOLDER)/CollectionReader.o

# Objects
$(BINFOLDER)/Registry.o: Registry.h Registry.cpp
	${COMPILE} Registry.cpp -o $(BINFOLDER)/Registry.o
$(BINFOLDER)/Run.o: Run.h Run.cpp
	${COMPILE} Run.cpp -o $(BINFOLDER)/Run.o
$(BINFOLDER)/Lexon.o: Lexon.h Lexon.cpp
	${COMPILE} Lexon.cpp -o $(BINFOLDER)/Lexon.o
$(BINFOLDER)/Parser.o: Parser.h Parser.cpp
	${COMPILE} Parser.cpp -o $(BINFOLDER)/Parser.o
$(BINFOLDER)/IndexBuilder.o: IndexBuilder.h IndexBuilder.cpp
	${COMPILE} IndexBuilder.cpp -o $(BINFOLDER)/IndexBuilder.o
$(BINFOLDER)/Index.o: Index.h Index.cpp
	${COMPILE} Index.cpp -o $(BINFOLDER)/Index.o
$(BINFOLDER)/Encoder.o: Encoder.h Encoder.cpp
	${COMPILE} Encoder.cpp -o $(BINFOLDER)/Encoder.o
$(BINFOLDER)/QueryResult.o: QueryResult.h QueryResult.cpp
	${COMPILE} QueryResult.cpp -o $(BINFOLDER)/QueryResult.o
$(BINFOLDER)/TimeMeasurement.o: TimeMeasurement.h TimeMeasurement.cpp
	${COMPILE} TimeMeasurement.cpp -o $(BINFOLDER)/TimeMeasurement.o
$(BINFOLDER)/QueryMain.o: QueryMain.cpp
	${COMPILE} QueryMain.cpp -o $(BINFOLDER)/QueryMain.o
$(BINFOLDER)/IndexMain.o: IndexMain.cpp
	${COMPILE} IndexMain.cpp -o $(BINFOLDER)/IndexMain.o
$(BINFOLDER)/DocumentInfo.o: DocumentInfo.cpp DocumentInfo.h
	${COMPILE} DocumentInfo.cpp -o $(BINFOLDER)/DocumentInfo.o
$(BINFOLDER)/DocumentIndex.o: DocumentIndex.cpp DocumentIndex.h
	${COMPILE} DocumentIndex.cpp -o $(BINFOLDER)/DocumentIndex.o
$(BINFOLDER)/AnchorText.o: AnchorText.cpp AnchorText.h
	${COMPILE} AnchorText.cpp -o $(BINFOLDER)/AnchorText.o

# htmlcxx
htmlcxx:
	cd $(HTMLCXX_PATH) && \
		./configure --prefix=$(CURRENT_DIR)/INST --exec-prefix=$(CURRENT_DIR)/BIN && \
		make clean && \
		make
	cd ..
	@mkdir -pv $(HTMLCXX_INCLUDE_PATH)/htmlcxx/html
	cp $(HTMLCXX_PATH)/html/*.h $(HTMLCXX_INCLUDE_PATH)/htmlcxx/html/
	cp $(HTMLCXX_PATH)/html/ParserSax.tcc $(HTMLCXX_INCLUDE_PATH)/htmlcxx/html/
	cp $(HTMLCXX_PATH)/html/.libs/*.o $(HTMLCXX_LIB_PATH)
	cd $(HTMLCXX_PATH) && \
		make distclean
htmlcxx_clean:
	rm -rfv $(HTMLCXX_INCLUDE_PATH)/*
	rm -rfv $(HTMLCXX_LIB_PATH)/libhtmlcxx*.o


# all
all:	$(INDEX_TARGET) $(QUERY_TARGET)

# Index
index:	$(INDEX_TARGET)

# Query
query:	$(QUERY_TARGET)

# run
run:	$(INDEX_TARGET) $(QUERY_TARGET)
	./bin/index -i indexToCompressedColection.txt -f test/input/toyExample -t test/output/ \
		-a test/output/index -v test/output/vocabulary -d test/output/documents \
		-p test/output/links -l test/output/anchorTexts
	./bin/query -a test/output/index -v test/output/vocabulary -d test/output/documents

# clean
clean:
	rm -f $(BINFOLDER)/*
	rm -f $(TESTFOLDER)/output/*
